<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            height: 100px;
            width: 100px;
            background-color: black;
            position: absolute;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        const box = document.querySelector(".box")
        

        box.onmousedown = function (e) {
            e = e;   
            let boxX = e.offsetX
            let boxY = e.offsetY
            box.onmousemove = function (e) {
                e = e
                let winX = e.clientX
                let winY = e.clientY
                box.style.left = `${winX - boxX}px`
                box.style.top = `${winY - boxY}px`
                console.log(`相对于浏览器窗口鼠标的位置：${winX},${winY}`)
                console.log(`相对于目标盒子原点鼠标的位置：${boxX},${boxY}`)
            }
            box.onmouseup = function () {
                box.onmousedown = null;
                box.onmousemove = null;
            }
        }

    </script>
</body>
</html>